﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp_SmartParking_Entities;
using WpfApp_SmartParking_IService;

namespace WpfApp_SmartParking_Service;

public class MenuService : BaseService,IMenuService
{
    public MenuService(DbContext dbContext):base(dbContext)
    {
            
    }

    public IEnumerable<SysMenu> GetMenuList(string key = "")
    {
        //return this.Query<SysMenu>(m => 1 == 1);

        return this.Query<SysMenu>(m =>

            string.IsNullOrEmpty(key) ? 1 == 1 :


            /// 有条件的时候，检查每个子项的时候，需要同时检查其子项
            /// 
            /// 检查当前菜单项的Header有没有包括关键词
            (m.MenuHeader.Contains(key) ||
            /// 检查当前菜单项的TargetView有没有包括关键词
            m.TargetView.Contains(key) ||
            /// 检查当前菜单项的子项中有没有符合条件的：Header或者TargetView包括了关键词
            Context.Set<SysMenu>().Where(sm => sm.ParentId == m.MenuId &&
                                    (sm.MenuHeader.Contains(key) || sm.TargetView.Contains(key))).Count() > 0
            )
            );
    }
}
